#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListWidgetItem>
#include <QLabel>
#include <QFrame>
#include <QToolButton>
#include <QTableWidget>
#include "statusmessagewidget.h"
#include "networkcardwidget.h"
#include "networkinfo.h"
#include "networkcontrol.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class NetworkDiagnoseWidget;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void resizeEvent(QResizeEvent *event) override;
    bool eventFilter(QObject *obj, QEvent *event) override;

private slots:
    void onNetworkSelectionChanged();
    void onNetworkContextMenuRequested(const QPoint &pos);
    void onDisableNetwork();
    void onDiagnoseNetwork();
    void onRenameNetwork();
    void onStatusNetwork();
    void onSettingNetwork();
    void onEnableNetwork();
    void onAddConnection();
    void onRefreshView();
    void showNetworkStatusDialog();
    
    // NetworkInfo相关槽函数
    void onNetworkInfoUpdated();
    void onNetworkError(const QString &error);

    // 绑定功能
    void onBondNetworks(const QList<ConnectionInfo>& selectedConns);
    void onUnbondNetwork(const ConnectionInfo& bondConn);
    bool createBondInterface(const QString& bondName, const QString& bondMode, 
                           int miiInterval, int upDelay, int downDelay, 
                           const QList<QString>& devices, QString& errorMsg);
    bool configureBondSlaves(const QString& bondName, const QList<QString>& devices, QString& errorMsg);
    bool unbondInterface(const QString& bondName, QString& errorMsg);
    void refreshNetworkInfo();

    // 桥接功能
    void onBridgeNetworks(const QList<ConnectionInfo>& selectedConns);
    void onUnbridgeNetwork(const ConnectionInfo& bridgeConn);
    bool createBridgeInterface(const QString& bridgeName, bool stpEnabled, int stpPriority,
                             int forwardDelay, int helloTime, int maxAge,
                             const QList<QString>& devices, QString& errorMsg);
    bool configureBridgeSlaves(const QString& bridgeName, const QList<QString>& devices, QString& errorMsg);
    bool unbridgeInterface(const QString& bridgeName, QString& errorMsg);

    void onEditBondMembers(const ConnectionInfo& bondConn);
    void onEditBridgeMembers(const ConnectionInfo& bridgeConn);

public slots:
    void showAboutDialog();

private:
    QAction* aboutAct;
    void updateStatusBar(int selectedCount = -1);
    void loadNetworkConnections(); // 加载网络连接数据
    void updateNetworkList(); // 更新网络列表显示
    void updateTableView(); // 更新表格视图
    QList<ConnectionInfo> findProfiles(const QString &devName, const QString &connName); // 统一profile查找
    
    Ui::MainWindow *ui;
    StatusMessageWidget *statusMessageWidget;
    QToolButton *listViewButton;
    QToolButton *tileViewButton;
    NetworkCardWidget::ViewMode m_currentViewMode;
    QTableWidget *tableWidgetNetwork = nullptr;
    
    // NetworkInfo相关成员
    NetworkInfo *m_networkInfo;
    QList<ConnectionInfo> m_connections;
    NetworkControl *m_networkControl = nullptr;

    // 记录右键菜单操作的item
    QListWidgetItem* m_contextMenuListItem = nullptr;
    int m_contextMenuTableRow = -1;
    QString m_contextMenuProfileName;

    bool canBondUI(const QList<ConnectionInfo>& conns);
    bool canBridgeUI(const QList<ConnectionInfo>& conns);
    QString getNetworkSegment(const QString& ip, const QString& mask);
};

#endif // MAINWINDOW_H